package com.huyaanalysisplatform.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.huyaanalysisplatform.common.Result;
import com.huyaanalysisplatform.entity.TVideo;
import com.huyaanalysisplatform.mapper.TVideoMapper;
import com.huyaanalysisplatform.service.TVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Huadi Group 12
 * @since 2021-08-01
 */
@Service
public class TVideoServiceImpl extends ServiceImpl<TVideoMapper, TVideo> implements TVideoService {

    @Autowired
    TVideoMapper tVideoMapper;

    @Override
    public Map<String,List<TVideo>> rankOfVideo() {
        List<TVideo> tVideos = tVideoMapper.rankOfVideo();
        Map<String, List<TVideo>> map = new HashMap<>();
        map.put("video", tVideos);
        return map;
    }

    @Override
    public Result add(TVideo tVideo) {
        if (tVideo.getViewCount() < 0){
            return Result.error().code(3001);
        }
        if (tVideo.getScore() < 0){
            return Result.error().code(4001);
        }
        TVideo sameTitle = tVideoMapper.selectByTitle(tVideo.getTitle());
        if (sameTitle != null){
            return Result.error().code(5001);
        }
        tVideo.setId(UUID.randomUUID().toString().replace("-",""));
        this.save(tVideo);
        return Result.success().code(2001);
    }

    @Override
    public Result edit(TVideo tVideo) {
        if (tVideo.getViewCount()<0){
            return Result.error().code(3001);
        }
        if (tVideo.getScore()<0){
            return Result.error().code(4001);
        }
        TVideo tVideoEo = tVideoMapper.selectById(tVideo.getId());
        if (!tVideoEo.getTitle().equals(tVideo.getTitle())){
            TVideo sameTitle = tVideoMapper.selectByTitle(tVideo.getTitle());
            if (sameTitle != null){
                return Result.error().code(5001);
            }
        }
        this.updateById(tVideo);
        return Result.error().code(2001);
    }

    @Override
    public Result delete(String id) {
        tVideoMapper.deleteById(id);
        return Result.success().code(2001);
    }
}
